package com.atguigu.gmall.common.demo;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Consumer01 {

    public static void main(String[] args) {
            consumer();
    }

    @SneakyThrows
    public static void consumer(){
        ConnectionFactory factory = new ConnectionFactory();
        List<Person> list = new ArrayList<>();
        factory.setVirtualHost("/");
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("XXXX");
        factory.setPassword("XXXX");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("queue",true,false,false,null);
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String s = body.toString();
                Person person = JSONObject.parseObject(s, Person.class);
                list.add(person);


            }

        };
        channel.basicConsume("queue",true,consumer);

    }
}
